﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Data.Linq;
using System.Linq;
using Microsoft.Practices.Unity;
using eCRAM.Model;
using System.Collections.Generic;

namespace eCRAM.Dao
{
    /// <summary>
    /// Allows to manipulate the CRAMs that are inside the SQL database.
    /// </summary>
    /// 
    /// <remarks>
    /// CRAM means in french Compte-Rendu d'Activité Mensuel i.e Monthly Activity Report
    /// A CRAM contains an id, a date, a state and a link to a user.
    /// Check CramModel.dbml for more informations.
    /// </remarks>
    public class CramRepository : LinqRepository<Cram>, ICramRepository
    {
        private Table<Cram> CramsTable { get; set; }

        [Dependency]
        public IRepository<CramLine> CramLineRepo { get; set; }

        public CramRepository(DataContext dataContent)
            : base(dataContent)
        {
            CramsTable = DataContext.GetTable<Cram>();
        }

        public override IQueryable<Cram> GetAll()
        {
            return (from cram in CramsTable select cram);
        }

        public override void Create(Cram cram)
        {
            CramsTable.InsertOnSubmit(cram);
            SubmitChanges();
        }

      /*  public override void Update(Cram cram)
        {
            Cram tmp = (CramsTable.Where(p => p.Id == cram.Id)).Single();
            tmp.Comment = cram.Comment;
            tmp.Status = cram.Status;
            tmp.LastModificationDate = DateTime.Now;
            //FIXME CramLines?
           
            //Delete old Cramlines
            foreach (CramLine cramline in tmp.CramLines)
            {
                CramLineRepo.Delete(cramline);
                tmp.DeleteCramLine(cramline);
                //CramLineRepo = new CramLineRepository();//FIXME
               
            }

            //Add new cramlines
            foreach (CramLine cramline in cram.CramLines)
            {
                //CramLineRepo = new CramLineRepository();//FIXME
                tmp.AddCramLine(cramline);
               CramLineRepo.Create(cramline);
               
            }

            SubmitChanges();
        }
       * */

        public override void Delete(Cram cram)
        {
            Table<CramLine> cramLinesTable = DataContext.GetTable<CramLine>();
            foreach (var cramLine in cram.CramLines)
            {
                cramLinesTable.DeleteOnSubmit(cramLine);
            }

            CramsTable.DeleteOnSubmit(cram);
        }

        public override Cram GetById(Guid CramId)
        {
            Cram cram = (CramsTable.Where(c => c.Id == CramId)).Single();
            return cram;
        }

        public Cram FindCram(Guid userId, int month, int year)
        {
            return CramsTable.Where(c => c.Year == year && c.MonthId == month && c.UserId == userId).FirstOrDefault<Cram>();
            
        }

        public override int GetCount()
        {
            return (from cram in CramsTable select cram).Count();
        }

        public override IQueryable<Cram> GetTop(int start, int limit)
        {
            IQueryable<Cram> crams = (
                                       from cram
                                       in CramsTable
                                       select cram
                                    )
                                    .Skip(start)
                                    .Take(limit - start);
            return crams;
        }
    }
}
